package com.mani.geotag;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.view.WindowManager;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;


public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> implements com.google.android.maps.ItemizedOverlay.OnFocusChangeListener {

	String TAG = "CustomItemizedOverlay";
	TrackBallFocusListener listener;
	private Context mContext;
	Bitmap mArrow;
	ArrayList<GeoPoint> mGeoPoints;
	Paint mPaint;
	Paint mCirclePaint;
	Paint mBorderPaint;
	Paint mCirclePaint1;
	Paint mBorderPaint1;
	Paint mArrowPaint;
	RectF mInnerCircle;
	RectF mOuterCircle;
	RectF mInnerCircle1;
	RectF mOuterCircle1;
	Rect mArrowRect;
	int mOveryLayItemsCount=0;
	int mHeight;
	interface TrackBallFocusListener{
		public void focused(int itemIndex);
	}
	ArrayList<OverlayItem> overlays = new ArrayList<OverlayItem>();
	int focusCounter = 0;
	
	public CustomItemizedOverlay(Drawable defaultMarker,Context context) {
		super(boundCenterBottom(defaultMarker));
		mContext = context;

		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setColor(Color.argb(0x20, 0x00, 0x00, 0xff));
		mPaint.setStyle(Paint.Style.FILL_AND_STROKE);

		mArrowPaint = new Paint();
		mArrowPaint.setAntiAlias(true);
		mArrowPaint.setAlpha(100);
		mArrowPaint.setStyle(Paint.Style.FILL_AND_STROKE);

		mCirclePaint = new Paint();
		mCirclePaint.setAntiAlias(true);
		mCirclePaint.setColor(Color.rgb(0x68,0xdb,0xf8));
		mCirclePaint.setStyle(Paint.Style.FILL_AND_STROKE);
		
		mBorderPaint = new Paint();
		mBorderPaint.setAntiAlias(true);
		mBorderPaint.setStrokeWidth(3f);
		mBorderPaint.setColor(Color.rgb(0x92,0x93,0xf3));
		mBorderPaint.setStyle(Paint.Style.STROKE);

		mCirclePaint1 = new Paint();
		mCirclePaint1.setAntiAlias(true);
		mCirclePaint1.setStrokeWidth(4f);
		mCirclePaint1.setColor(Color.argb(0x80,0xc1,0xd1,0xd4));
		mCirclePaint1.setStyle(Paint.Style.STROKE);
		
		mBorderPaint1 = new Paint();
		mBorderPaint1.setAntiAlias(true);
		mBorderPaint1.setStrokeWidth(2f);
		mBorderPaint1.setColor(Color.rgb(0x47,0x47,0x47));
		mBorderPaint1.setStyle(Paint.Style.STROKE);

		mInnerCircle = new RectF();
		mOuterCircle = new RectF();
		mInnerCircle1 = new RectF();
		mOuterCircle1 = new RectF();
		//mArrowRect = new Rect(305,200,353,248);
		//mArrow = BitmapFactory.decodeResource(context.getResources(), R.drawable.arrow);
		DisplayMetrics metrics = new DisplayMetrics();
    	((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics);
		mHeight = metrics.heightPixels;
		if(mHeight == 320 || mHeight == 240) {
			 mInnerRadius = 3;
			 mOuterRadius = 20;
			 mInnerRadius1 = 7;
			 mOuterRadius1 = 10;	
			 mRadius = 19;
		} else {
			 mInnerRadius = 6;
			 mOuterRadius = 35;
			 mInnerRadius1 = 10;
			 mOuterRadius1 = 12;	
			 mRadius = 33;
		}
		setOnFocusChangeListener(this);
	}
	
	public CustomItemizedOverlay(Drawable defaultMarker, TrackBallFocusListener listener) {
		super(boundCenterBottom(defaultMarker));
		setOnFocusChangeListener(this);
		this.listener = listener;
	}

	public Drawable boundToCenterBottom(Drawable d){
		return super.boundCenter(d);
	}
	public void addOverlay(OverlayItem item){
		overlays.add(item);
		// call populate, which internally call createItem
		populate();
	}
	
	public OverlayItem getOverlayItem(int index){
		return overlays.get(index);
	}
	
	@Override
	protected OverlayItem createItem(int index) {
		// TODO Auto-generated method stub
		return overlays.get(index);
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		return overlays.size();
	}

/*	@Override
	public boolean onTap(GeoPoint p, MapView mapView) {
		Log.i(TAG, "onTap: "+ p.toString());
		return super.onTap(p, mapView);
	}

	 protected boolean onTap(int index)
	 {
		 OverlayItem item = overlays.get(index);
		/* MapsMarkerDialog dialog = new MapsMarkerDialog(mContext);
		 dialog.setTitle(item.getTitle());
		 dialog.setMessage(item.getSnippet());
		 dialog.show(); 
		 return true; 
	 } */
	 public void onFocusChanged(ItemizedOverlay overlay, OverlayItem newFocus) {
		Log.i(TAG, "onFocusChanged: ");
		if(newFocus != null)
			Log.i(TAG, "Focused Marker: "+ newFocus.getTitle());
	} 
	 
		int currX;
		boolean isMovementDetected = false;
		boolean isCheckDrawn = false;
		boolean isTouchDown = false;
		
		public boolean onTouchEvent(MotionEvent event) {
			System.out.println("On touch event *********** ");
			int pointerX = (int) event.getX();
			int pointerY = (int) event.getY();
			switch(event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					if(mArrowRect.contains(pointerX, pointerY) == true)
						isTouchDown = true;
					break;
				case MotionEvent.ACTION_MOVE:
					currX = (int) event.getX();
					int deltaX = Math.abs(currX - pointerX);
		
					if(deltaX > ViewConfiguration.getTouchSlop())
					{
						isMovementDetected = true;
					}
						
					break;
				case MotionEvent.ACTION_UP:
					if(isMovementDetected == false && isTouchDown == true) {
						((CircularActivity)mContext).moveToCurrentLocation();
						isTouchDown = false;
					}
					isMovementDetected = false;
					break;
			}
			return true;
		}
		
	 private int mInnerRadius;
	 private int mOuterRadius;
	 private int mInnerRadius1;
	 private int mOuterRadius1;	 
	 private int mRadius;
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean flag) {
		super.draw(canvas, mapView, flag);

		float x = 0;
		float y = 0;

		Projection projection = mapView.getProjection();
		OverlayItem item = overlays.get(0);
		Point point = projection.toPixels(item.getPoint(), null);
		x = point.x;
		y = point.y;
	
		mInnerCircle.left = point.x - mInnerRadius;
		mInnerCircle.top = point.y - mInnerRadius;
		mInnerCircle.right = point.x + mInnerRadius;
		mInnerCircle.bottom = point.y + mInnerRadius;

		mOuterCircle.left = point.x - mOuterRadius;
		mOuterCircle.top = point.y - mOuterRadius;
		mOuterCircle.right = point.x + mOuterRadius;
		mOuterCircle.bottom = point.y + mOuterRadius;

		mInnerCircle1.left = point.x - mInnerRadius1;
		mInnerCircle1.top = point.y - mInnerRadius1;
		mInnerCircle1.right = point.x + mInnerRadius1;
		mInnerCircle1.bottom = point.y + mInnerRadius1;

		mOuterCircle1.left = point.x - mOuterRadius1;
		mOuterCircle1.top = point.y - mOuterRadius1;
		mOuterCircle1.right = point.x + mOuterRadius1;
		mOuterCircle1.bottom = point.y + mOuterRadius1;

		// Draw the circles
		canvas.drawCircle(x, y, mRadius, mPaint);
		canvas.drawOval(mInnerCircle, mCirclePaint);
		canvas.drawOval(mInnerCircle1, mCirclePaint1);
		canvas.drawOval(mOuterCircle1,mBorderPaint1);
		canvas.drawOval(mOuterCircle,mBorderPaint);
		
		//canvas.drawBitmap(mArrow, 305,200, mArrowPaint);
		
	}

}
